package org.example.handler;

import lombok.extern.slf4j.Slf4j;
import org.example.common.exception.MyExampleException;
import org.example.common.lang.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MyExampleException.class)
    public Result handleException(MyExampleException e){
        return Result.fail(e.getMessage());
    }


    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        log.error("报错信息：",e.getMessage());
//        Map<String,Object> map = new HashMap<>();
//        map.put("code",e.getCode());
//        map.put("msg",e.getMsg());
        return Result.fail(e.getMessage());
    }
}
